import 'dart:ui';
import 'dart:math';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';

class RadarViewModel {
  final String text;
  final double value;
  final Color color;

//  final Function(String sysName) onTap;
  RadarViewModel({this.text, this.value, this.color});
}

class RadarView extends CustomPainter {
  double radius;
  Color userColor;
  Color bgColor;
  List<RadarViewModel> dataList;

  RadarView({this.radius, this.dataList, this.userColor, this.bgColor})
      : assert(dataList != null),
        assert(radius > 0);

  Paint _paint = Paint()
    ..color = Color.fromRGBO(121, 117, 255, 0.2)
    ..strokeCap = StrokeCap.round
    ..isAntiAlias = true
    ..strokeWidth = 1.0;

  @override
  void paint(Canvas canvas, Size size) {
    _paint.color = Color(0x6FFFFFFF);
    Offset center = Offset(size.width / 2.0, size.height / 2.0);
    int edge = dataList.length; //等分
    double radian = 2 * pi / edge; //弧度
    double radianCompensation = 2 * pi / 20; //补偿18度

    List<Offset> labellist = [];
    for (int i = 0; i <= 2; i++) {
      List<Offset> childlist = [];
      for (int j = 0; j < edge; j++) {
        if (i == 0) {
          childlist.add(Offset(
              i * cos(j * radian - radianCompensation) + center.dx,
              i * sin(j * radian - radianCompensation) + center.dy));
        } else if (i == 1) {
          ///在60%的地方画轴
          childlist.add(Offset(
              radius * 0.6 * cos(j * radian - radianCompensation) + center.dx,
              radius * 0.6 * sin(j * radian - radianCompensation) + center.dy));
        } else {
          childlist.add(Offset(
              radius * 1 * cos(j * radian - radianCompensation) + center.dx,
              radius * 1 * sin(j * radian - radianCompensation) + center.dy));
          labellist.add(Offset(
              (radius * 1 + setRatio(15)) *
                      cos(j * radian - radianCompensation) +
                  center.dx,
              (radius * 1 + setRatio(15)) *
                      sin(j * radian - radianCompensation) +
                  center.dy));
        }
      }
      childlist.add(childlist?.first);
      canvas.drawPoints(PointMode.polygon, childlist, _paint);

      if (i == 2) {
        //画背景
        Path path = Path();
        childlist.forEach((item) {
          path.lineTo(item.dx, item.dy);
        });
        canvas.drawPath(path, _paint..color = bgColor);
      }

      if (i == 2) {
        List<Offset> shaftList = [];
        shaftList.addAll(childlist);
        //画轴
        for (int i = 0; i < childlist.length; i++) {
          //0 1 //1 3 //2 5 //3 7 //4 9
          shaftList.insert((i * 2) + 1, center);
        }
        canvas.drawPoints(PointMode.polygon, shaftList, _paint);
      }
    }
    //绘制用户区域
    List<Offset> arealist = [];
    for (int i = 0; i < dataList.length; i++) {
      arealist.add(Offset(
          dataList[i].value * cos(i * radian - radianCompensation) + center.dx,
          dataList[i].value * sin(i * radian - radianCompensation) +
              center.dy));
    }
    arealist.add(arealist?.first);
    Path path = Path()..moveTo(arealist?.first?.dx, arealist?.first?.dy);
    arealist.forEach((item) {
      path.lineTo(item.dx, item.dy);
    });
    canvas.drawPath(path, _paint..color = userColor);

    //绘制文字
    _paintLabel(canvas, size, labellist);
  }

  void _paintLabel(Canvas canvas, Size size, List<Offset> labellist) {
    for (int i = 0; i < labellist.length; i++) {
      RadarViewModel model = dataList[i];
      Offset labelOffset = labellist[i];
      if (labelOffset.dx.ceil() < (size.width / 2.0).ceil()) {
        labelOffset = Offset(
            labelOffset.dx - setRatio(50), labelOffset.dy - setRatio(10));
      } else {
        if (labelOffset.dx.ceil() == (size.width / 2.0).ceil()) {
          //特殊处理顶上那个平分的点
          labelOffset = Offset(
              labelOffset.dx - setRatio(28), labelOffset.dy - setRatio(30));
        } else {
          labelOffset = Offset(
              labelOffset.dx - setRatio(10), labelOffset.dy - setRatio(10));
        }
      }
      TextPainter(
          text: TextSpan(
              style: TextStyle(fontSize: setTextSp(14), color: model.color),
              // recognizer: TapGestureRecognizer()
              //   ..onTap = () {
              //     //不生效
              //     model.onTap(model.text);
              //   },
              children: [
                TextSpan(
                    text: model.text.split('\n').first,
                    style: TextStyle(color: Color(0xFFFFDD80))),
                TextSpan(text: '\n'),
                TextSpan(text: model.text.split('\n').last)
              ]),
          textDirection: TextDirection.ltr,
          textAlign: TextAlign.center)
        ..layout(maxWidth: size.width, minWidth: 0.0)
        ..paint(canvas, labelOffset);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
